// Author: wsfuyibing <682805@qq.com>
// Date: 2025-06-30

package detail

import (
	"context"
	"gitee.com/go-libs/db-xorm/db"
	"server/app/errs"
	"server/app/http/models"
	"server/app/http/responses"
	"server/app/http/services"
	"server/app/kind"
)

// Logic
// 详情逻辑
type Logic struct{}

// GetArcade
// 读取钓场.
func (o *Logic) GetArcade(ctx context.Context, id kind.ArcadeId) (arcade *models.Arcades, err error) {
	var has bool
	if arcade, has, err = services.NewArcadesService().Dao.GetById(ctx, id); err != nil {
		err = errs.ErrDatabaseQuery
	} else if !has {
		err = errs.ErrArcadeNotFound
	}
	return
}

// GetPond
// 读取鱼塘.
func (o *Logic) GetPond(ctx context.Context, id int64) (pond *models.ArcadePond, err error) {
	var has bool
	if pond, has, err = services.NewArcadePondService().Dao.GetById(ctx, id); err != nil {
		err = errs.ErrDatabaseQuery
	} else if !has || pond.Deleted.IsYes() {
		err = errs.ErrArcadePondNotFound
	}
	return
}

// GetSession
// 读取场次.
func (o *Logic) GetSession(ctx context.Context, id int64) (session *models.TicketSession, err error) {
	var has bool
	if session, has, err = services.NewTicketSessionService().Dao.GetById(ctx, id); err != nil {
		err = errs.ErrDatabaseQuery
	} else if !has || session.Deleted.IsYes() {
		err = errs.ErrTicketSessionNotFound
	}
	return
}

// GetTicket
// 读取渔票.
func (o *Logic) GetTicket(ctx context.Context, id int64) (ticket *models.Ticket, err error) {
	var has bool
	if ticket, has, err = services.NewTicketService().Dao.GetById(ctx, id); err != nil {
		err = errs.ErrDatabaseQuery
	} else if !has || ticket.Deleted.IsYes() {
		err = errs.ErrTicketNotFound
	}
	return
}

// ListFreeGoods
// 读取赠品.
func (o *Logic) ListFreeGoods(ctx context.Context, id int64) (list []*models.Goods, err error) {
	var (
		free []*models.TicketFreeGoods
		ids  = make([]kind.GoodsId, 0)
	)
	if free, err = services.NewTicketFreeGoodsService().Dao.ListByMap(ctx, map[string]any{
		"session_id": id,
	}); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else {
		for _, v := range free {
			if v.GoodsId > 0 {
				ids = append(ids, v.GoodsId)
			}
		}
	}
	if list, err = services.NewGoodsService().ListByIdList(ctx, ids); err != nil {
		err = errs.ErrDatabaseQuery
	}
	return
}

// ListFreeGoodsResponse
// 读取赠品.
func (o *Logic) ListFreeGoodsResponse(ctx context.Context, id int64) (list []*responses.TicketFreeGoodsResponse, err error) {
	var (
		free   []*models.TicketFreeGoods
		ids    = make([]kind.GoodsId, 0)
		mapper = make(map[kind.GoodsId]*models.Goods)
	)

	list = make([]*responses.TicketFreeGoodsResponse, 0)

	if free, err = services.NewTicketFreeGoodsService().Dao.ListByMap(ctx, map[string]any{
		"session_id": id,
	}); err != nil {
		err = errs.ErrDatabaseQuery
		return
	} else {
		for _, v := range free {
			if v.GoodsId > 0 {
				ids = append(ids, v.GoodsId)
				list = append(list, (&responses.TicketFreeGoodsResponse{}).With(v))
			}
		}
	}

	if mapper, err = services.NewGoodsService().MapByIdList(ctx, ids); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	for _, v := range list {
		if m, ok := mapper[v.GoodsId]; ok {
			v.WithGoods(m)
		}
	}

	return
}

// ListSession
// 场次列表.
func (o *Logic) ListSession(ctx context.Context, id int64) (list []*models.TicketSession, err error) {
	var sess *db.Session

	// 1. 建立连接.
	if sess, err = db.Config.GetSlave(ctx); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 2. 读取列表.
	if err = sess.Where("ticket_id = ? AND state = ? AND date >= ?",
		id,
		kind.TicketStateEnabled,
		db.NewDate(),
	).Find(&list); err != nil {
		err = errs.ErrDatabaseQuery
	}
	return
}
